// import './index.scss';
export default ngModule => {
    ngModule.component('csfTable', {
        template: require('./index.html'),
        bindings: {
            columns: '=',
            data: '=',
        },
        controllerAs: 'mainCtr',
        controller($scope, $compile, $timeout) {
            this.isCheckedAll = false;
            this.checkedAll = () => {
                $timeout(() => {
                    _.forEach(this.data, item => {
                        item.checked = this.isCheckedAll;
                    });
                });
            }
            this.checkedItem = () => {
                $timeout(() => {
                    let flag = true;
                    _.forEach(this.data, item => {
                        if (!item.checked) {
                            flag = false;
                            return false;
                        }
                    });
                    this.isCheckedAll = flag;
                })
            }
            this.bindOperate = () => {
                this.operate = {};
                _.forEach(this.columns, item => {
                    if (item.operate) {
                        _.forEach(item.operate, subItem => {
                            this.operate[subItem.name] = subItem;
                        });
                    }
                })
            }
            this.renderBody = () => {
                _.forEach(this.data, d => {
                    _.forEach(this.columns, c => {
                        let { key } = c;
                        if (!d[key]) {
                            if (c.render) {
                                d[key] = $compile(c.render())($scope);
                            } else if (c.formatter) {
                                d[key] = c.formatter;
                            }
                        }
                    })
                });
            }

            this.$onInit = () => {
                this.showCheckbox = this.columns[0].showCheckbox;
                this.columns = this.columns.slice(1);
                this.bindOperate();
                this.renderBody();
            }
            this.$onChanges = () => {

            }
        }
    })
}